package com.littlepay.kr4.paulin.dfeba77.utils;

import com.littlepay.kr4.paulin.dfeba77.application.MyApplication;
import com.littlepay.kr4.paulin.dfeba77.config.SqliteConfig;
import com.littlepay.kr4.paulin.dfeba77.entity.Sticker;

import java.util.List;

/**
 * Created by 77dfeba on 2017/6/7.
 * C代表cost花费，S代表surplus剩余
 * 该函数用于计算每日的剩余和今日消费
 */

public class CSCalculators {
    public static double costMonth;
    public static double surplusMonth;
    public static double costDay;
    public static double surplusDay;


    private CSCalculators(){}

    public static double getMonthCost(){
        double costMonth = 0;
        MyApplication myApplication = MyApplication.getInstance();
        SqliteHelper sqliteHelper = new SqliteHelper(myApplication.getContext() , SqliteConfig.SQLITE_DATABASE_FILE_NAME , null ,1);
        List<Sticker> stickersListMonth = sqliteHelper.queryDataListByDay(DateUtil.getMonthDate());
        for (int i=0;i<stickersListMonth.size();i++){
            costMonth += stickersListMonth.get(i).getCost();
        }
        return costMonth;
    }
    public static double getMonthSurplus(){
        //MyApplication myApplication = MyApplication.getInstance();
        return Double.parseDouble(PrefUtils.getBudget()) - getMonthCost();
    }
    private static double getDayCost(){
        double costDay = 0;
        MyApplication myApplication = MyApplication.getInstance();
        SqliteHelper sqliteHelper = new SqliteHelper(myApplication.getContext() , SqliteConfig.SQLITE_DATABASE_FILE_NAME , null ,1);
        List<Sticker> stickersListMonth = sqliteHelper.queryDataListByDay(DateUtil.getTodayDate());
        for (int i=0;i<stickersListMonth.size();i++){
            costDay += stickersListMonth.get(i).getCost();
        }
        return costDay;
    }
    private static double getDaySurplus(){
        double surplusDay = 0;
        surplusDay = (Double.parseDouble(PrefUtils.getBudget()) - getMonthCost()) / DateUtil.getMonthLeftDay() - getDayCost();
        return surplusDay;
    }
    public static void refreshAll(){
        costMonth = getMonthCost();
        surplusMonth = getMonthSurplus();
        costDay = getDayCost();
        surplusDay = getDaySurplus();
    }
}
